﻿using System;
using System.Collections.Generic;
using DeHeng_Mes.Constant;
using S7.Net;
using System.Management;
using DeHeng_Mes.Model.PlcModel.P3;
using DeHeng_Mes.Model.PlcModel.P2;
using DeHeng_Mes.Model.PlcModel.P1;
using DeHeng_Mes.Model.PlcModel;
using Org.BouncyCastle.Crypto.Modes;
using DeHeng_Mes.Model.PlcModel.P4;
using DeHeng_Mes.Model.PlcModel.P5;
using DeHeng_Mes.Model.PlcModel.Revet;

namespace DeHeng_Mes.Utils
{
    public static class MappingTable
    {
        /// <summary>
        /// plc数据与实体类的映射
        /// </summary>
        /// <param name="plc"></param>
        /// <param name="plcName"></param>
        /// <returns></returns>
        public static Dictionary<object, Type> PlcMapping(Plc plc, string plcName)
        {
            // 初始化返回值
            Dictionary<object, Type> dic = new Dictionary<object, Type>();

            // 定义一个类型和地址的映射表
            var typeAddressMap = new Dictionary<string, List<(Type type, int address)>>
            {
                { "P3", new List<(Type, int)>
                    {
                        (typeof(P3Power), 6002),
                        (typeof(P3Production), 6001),
                        (typeof(P3QRResult), 6004),
                    }
                },
                { "P4", new List<(Type, int)>
                    {
                        (typeof(P4Power), 6002),
                        (typeof(P4Production), 6001),
                        (typeof(P4QRResult), 6004),
                    }
                },
                { "P5", new List<(Type, int)>
                    {
                        (typeof(P5Power), 6002),
                        (typeof(P5Production), 6001),
                        (typeof(P5QRResult), 6004),
                    }
                },
                { "Rivet", new List<(Type, int)>
                    {
                        (typeof(RivetQRResult), 6004),
                    }
                }
            };

            // 定义读取数据并添加到字典的方法
            void AddInstance(Type type, int address)
            {
                // 引用ReadClass方法的命名空间和类
                var method = typeof(Plc).GetMethod("ReadClass", new Type[] { typeof(int), typeof(int) })?.MakeGenericMethod(type);
                var instance = method.Invoke(plc, new object[] { address, 0 });
                dic.Add(instance, type);
            }

            try
            {
                // 检查并处理对应的Name类型
                if (typeAddressMap.ContainsKey(plcName))
                {
                    foreach (var (type, address) in typeAddressMap[plcName])
                    {
                        AddInstance(type, address);
                    }
                }

                return dic;
            }
            catch (Exception ex)
            {
                Tools.ProduceLogTextError($"{plcName}获取Plc数据实例与实例类型映射失败，原因是：" + ex.Message);
            }

            return dic;
        }

        /// <summary>
        /// 获取Cpu唯一标识
        /// </summary>
        public static string DeviceIdentifier()
        {
            string cpuId = string.Empty;
            using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("select ProcessorId from Win32_Processor"))
            {
                foreach (ManagementObject obj in searcher.Get())
                {
                    cpuId = obj["ProcessorId"].ToString();
                    break;
                }
            }
            return cpuId;
        }

        /// <summary>
        /// 设备与数据库账号映射表
        /// </summary>
        /// <param name="cpuIp"></param>
        /// <returns></returns>
        public static (string, string) GetSqlUser(string cpuIp)
        {
            string userName = "";
            string passWord = "";
            //BFEBFBFF000906EA
            //BFEBFBFF000906EA
            Dictionary<string, Dictionary<string, string>> dic = new Dictionary<string, Dictionary<string, string>>()
            {
                {"BFEBFBFF000906EA1",new Dictionary<string, string>()
                {
                    { "sa","yk19991223"}
                } },
                {"BFEBFBFF000806EB",new Dictionary<string, string>()
                {
                    { "root","265422"}
                } }
            };

            foreach (var item in dic)
            {
                if (item.Key.Equals(cpuIp))
                {
                    //解析Value
                    foreach (var item1 in item.Value)
                    {
                        userName = item1.Key;
                        passWord = item1.Value;
                    }
                }
            }
            userName = string.IsNullOrEmpty(userName) ? SystemConstant.MySqlName : userName;
            passWord = string.IsNullOrEmpty(passWord) ? SystemConstant.MySqlPassWord : passWord;
            return (userName, passWord);
        }

        /// <summary>
        /// 请求对照表
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string UrlAnalysis(string url)
        {
            Dictionary<string, string> dic
                = new Dictionary<string, string>()
                {
                    {"OrderInfo","ProcessRequest" },
                    {"SpotCheckOk","SpotCheckOk" },
                    {"OrderEnd","OrderEnd" },
                    {"AdditionalOrder","AdditionalOrder" },
                    {"ISVGlue","ISVGlue" },
                    {"GetNgCode","GetNgCode" },
                    {"AndonTTS","AndonTTS" },
                    {"AndonDelete","AndonDelete" },
                    {"ReportingForWork","ReportingForWork" },
                    {"receiveReport","receiveReport" },
                    {"checkReport","checkReport" },
                };
            foreach (var item in dic)
            {
                if (url.Contains(item.Key))
                {
                    return item.Value;
                }
            }
            return "";
        }

        /// <summary>
        /// 获取该类字段与Plc数据映射
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static Dictionary<string, Tuple<int, int>> GetDicByName(string key)
        {
            var dic = new Dictionary<string, Dictionary<string, Tuple<int, int>>>();

            #region 状态读取映射
            dic["P3-STATUS"] = new Dictionary<string, Tuple<int, int>>()
            {
                { SystemConstant.ST10A,new Tuple<int, int>(0,1)},
                { SystemConstant.ST10B,new Tuple<int, int>(1,1)},
            };

            dic["P4-STATUS"] = new Dictionary<string, Tuple<int, int>>()
            {
                { SystemConstant.ST20A,new Tuple<int, int>(0,1)},
                { SystemConstant.ST20B,new Tuple<int, int>(1,1)},
            };

            dic["P5-STATUS"] = new Dictionary<string, Tuple<int, int>>()
            {
                { SystemConstant.ST30A,new Tuple<int, int>(0,1)},
                { SystemConstant.ST30B,new Tuple<int, int>(1,1)},
            };

            dic["Rivet-STATUS"] = new Dictionary<string, Tuple<int, int>>()
            {
                { SystemConstant.ST05B,new Tuple<int, int>(0,1)},
            };
            #endregion

            dic[SystemConstant.ST10A] = new Dictionary<string, Tuple<int, int>>()
            {
                { SystemConstant.STATION,new Tuple<int, int>(0,10)},
                { SystemConstant.BATCH,new Tuple<int, int>(12,10)},
                { SystemConstant.TYPE,new Tuple<int, int>(24,10)},
                { SystemConstant.WORKER,new Tuple<int, int>(36,10)},
                { SystemConstant.RBNO,new Tuple<int, int>(48,10)},
                 { SystemConstant.CODE1,new Tuple<int, int>(32620,64)},
                 { SystemConstant.CODE2,new Tuple<int, int>(32684,64)},
                 { SystemConstant.CODE3,new Tuple<int, int>(32748,64)},
                 { SystemConstant.CODE4,new Tuple<int, int>(32812,64)},
                { SystemConstant.STARTTIME,new Tuple<int, int>(102,8)},
                { SystemConstant.ENDTIME,new Tuple<int, int>(110,8)},
                { SystemConstant.BEAT,new Tuple<int, int>(126,2)},
                { SystemConstant.RESULT,new Tuple<int, int>(128,1)},
            };

            dic[SystemConstant.ST10B] = new Dictionary<string, Tuple<int, int>>()
            {
                { SystemConstant.STATION,new Tuple<int, int>(13074,10)},
                { SystemConstant.BATCH,new Tuple<int, int>(13086,10)},
                { SystemConstant.TYPE,new Tuple<int, int>(13098,10)},
                { SystemConstant.WORKER,new Tuple<int, int>(13110,10)},
                { SystemConstant.RBNO,new Tuple<int, int>(13122,10)},
                 { SystemConstant.CODE1,new Tuple<int, int>(32876,64)},
                 { SystemConstant.CODE2,new Tuple<int, int>(32940,64)},
                 { SystemConstant.CODE3,new Tuple<int, int>(33004,64)},
                { SystemConstant.STARTTIME,new Tuple<int, int>(13176,8)},
                { SystemConstant.ENDTIME,new Tuple<int, int>(13184,8)},
                { SystemConstant.BEAT,new Tuple<int, int>(13200,2)},
                { SystemConstant.RESULT,new Tuple<int, int>(13202,1)},
            };

            dic[SystemConstant.ST20A] = new Dictionary<string, Tuple<int, int>>()
            {
                { SystemConstant.STATION,new Tuple<int, int>(0,10)},
                { SystemConstant.BATCH,new Tuple<int, int>(12,10)},
                { SystemConstant.TYPE,new Tuple<int, int>(24,10)},
                { SystemConstant.WORKER,new Tuple<int, int>(36,10)},
                { SystemConstant.RBNO,new Tuple<int, int>(48,10)},
                 { SystemConstant.CODE1,new Tuple<int, int>(29384,64)},
                 { SystemConstant.CODE2,new Tuple<int, int>(29448,64)},
                 { SystemConstant.CODE3,new Tuple<int, int>(29512,64)},
                 { SystemConstant.CODE4,new Tuple<int, int>(29576,64)},
                { SystemConstant.STARTTIME,new Tuple<int, int>(102,8)},
                { SystemConstant.ENDTIME,new Tuple<int, int>(110,8)},
                { SystemConstant.BEAT,new Tuple<int, int>(126,2)},
                { SystemConstant.RESULT,new Tuple<int, int>(128,1)},
            };

            dic[SystemConstant.ST20B] = new Dictionary<string, Tuple<int, int>>()
            {
                { SystemConstant.STATION,new Tuple<int, int>(13074,10)},
                { SystemConstant.BATCH,new Tuple<int, int>(13086,10)},
                { SystemConstant.TYPE,new Tuple<int, int>(13098,10)},
                { SystemConstant.WORKER,new Tuple<int, int>(13110,10)},
                { SystemConstant.RBNO,new Tuple<int, int>(13122,10)},
                 { SystemConstant.CODE1,new Tuple<int, int>(29640,64)},
                 { SystemConstant.CODE2,new Tuple<int, int>(29704,64)},
                 { SystemConstant.CODE3,new Tuple<int, int>(29768,64)},
                { SystemConstant.STARTTIME,new Tuple<int, int>(13176,8)},
                { SystemConstant.ENDTIME,new Tuple<int, int>(13184,8)},
                { SystemConstant.BEAT,new Tuple<int, int>(13200,2)},
                { SystemConstant.RESULT,new Tuple<int, int>(13202,1)},
            };

            dic[SystemConstant.ST30A] = new Dictionary<string, Tuple<int, int>>()
            {
                { SystemConstant.STATION,new Tuple<int, int>(0,10)},
                { SystemConstant.BATCH,new Tuple<int, int>(12,10)},
                { SystemConstant.TYPE,new Tuple<int, int>(24,10)},
                { SystemConstant.WORKER,new Tuple<int, int>(36,10)},
                { SystemConstant.RBNO,new Tuple<int, int>(48,10)},
                 { SystemConstant.CODE1,new Tuple<int, int>(52036,64)},
                 { SystemConstant.CODE2,new Tuple<int, int>(52100,64)},
                 { SystemConstant.CODE3,new Tuple<int, int>(52164,64)},
                 { SystemConstant.CODE4,new Tuple<int, int>(52228,64)},
                { SystemConstant.STARTTIME,new Tuple<int, int>(102,8)},
                { SystemConstant.ENDTIME,new Tuple<int, int>(110,8)},
                { SystemConstant.BEAT,new Tuple<int, int>(126,2)},
                { SystemConstant.RESULT,new Tuple<int, int>(128,1)},
            };

            dic[SystemConstant.ST30B] = new Dictionary<string, Tuple<int, int>>()
            {
                { SystemConstant.STATION,new Tuple<int, int>(26018,10)},
                { SystemConstant.BATCH,new Tuple<int, int>(26030,10)},
                { SystemConstant.TYPE,new Tuple<int, int>(26042,10)},
                { SystemConstant.WORKER,new Tuple<int, int>(26054,10)},
                { SystemConstant.RBNO,new Tuple<int, int>(26066,10)},
                 { SystemConstant.CODE1,new Tuple<int, int>(52292,64)},
                 { SystemConstant.CODE2,new Tuple<int, int>(52356,64)},
                 { SystemConstant.CODE3,new Tuple<int, int>(52420,64)},
                 { SystemConstant.CODE4,new Tuple<int, int>(52484,64)},
                { SystemConstant.STARTTIME,new Tuple<int, int>(26120,8)},
                { SystemConstant.ENDTIME,new Tuple<int, int>(26128,8)},
                { SystemConstant.BEAT,new Tuple<int, int>(26144,2)},
                { SystemConstant.RESULT,new Tuple<int, int>(26146,1)},
            };

            dic[SystemConstant.ST05B] = new Dictionary<string, Tuple<int, int>>()
            {
                { SystemConstant.STATION,new Tuple<int, int>(0,10)},
                { SystemConstant.BATCH,new Tuple<int, int>(12,10)},
                { SystemConstant.TYPE,new Tuple<int, int>(24,10)},
                { SystemConstant.WORKER,new Tuple<int, int>(36,10)},
                { SystemConstant.RBNO,new Tuple<int, int>(48,10)},
                 { SystemConstant.CODE1,new Tuple<int, int>(130,64)},
                 { SystemConstant.CODE2,new Tuple<int, int>(194,64)},
                 { SystemConstant.CODE3,new Tuple<int, int>(258,64)},
                { SystemConstant.STARTTIME,new Tuple<int, int>(102,8)},
                { SystemConstant.ENDTIME,new Tuple<int, int>(110,8)},
                { SystemConstant.BEAT,new Tuple<int, int>(126,2)},
                { SystemConstant.RESULT,new Tuple<int, int>(128,1)},
            };

            // 返回匹配数据
            if (dic.TryGetValue(key, out Dictionary<string, Tuple<int, int>> data))
                return data;

            return null;
        }

        /// <summary>
        /// 根据字符串数据获取相关DB数据的二元组
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static Tuple<int, int> GetBytesOff(string key)
        {
            var dic = new Dictionary<string, Tuple<int, int>>();
            #region 状态读取数组
            dic["P3-STATUS"] = new Tuple<int, int>(SystemConstant.PLC01_STATUS_DB, SystemConstant.PLC01_STATUS_ENDOFF);
            dic["P4-STATUS"] = new Tuple<int, int>(SystemConstant.PLC02_STATUS_DB, SystemConstant.PLC02_STATUS_ENDOFF);
            dic["P5-STATUS"] = new Tuple<int, int>(SystemConstant.PLC03_STATUS_DB, SystemConstant.PLC03_STATUS_ENDOFF);
            dic["Rivet-STATUS"] = new Tuple<int, int>(SystemConstant.PLCRIVET_STATUS_DB, SystemConstant.PLCRIVET_STATUS_ENDOFF);
            #endregion

            #region PLC01数据数组
            dic["P3-WELD"] = new Tuple<int, int>(SystemConstant.PLC01_WELD_DB, SystemConstant.PLC01_WELD_ENDOFF);
            #endregion

            #region PLC02数据数组
            dic["P4-WELD"] = new Tuple<int, int>(SystemConstant.PLC02_WELD_DB, SystemConstant.PLC02_WELD_ENDOFF);
            #endregion

            #region PLC03数据数组
            dic["P5-WELD"] = new Tuple<int, int>(SystemConstant.PLC03_WELD_DB, SystemConstant.PLC03_WELD_ENDOFF);
            #endregion

            #region PLCRIVET数据数组
            dic["Rivet-RIVET"] = new Tuple<int, int>(SystemConstant.PLCRIVET_RIVET_DB, SystemConstant.PLCRIVET_RIVET_ENDOFF);
            #endregion

            dic["P5-CHECK"] = new Tuple<int, int>(5061, 128);

            if (dic.TryGetValue(key, out Tuple<int, int> value))
                return value;
            return null;
        }

        /// <summary>
        /// 根据PlcName与StationName获取工艺名称
        /// </summary>
        /// <param name="plcName"></param>
        /// <param name="StationName"></param>
        /// <returns></returns>
        public static string GetOperationNo(string plcName, string StationName)
        {
            var dic = new Dictionary<string, Dictionary<string, string>>();

            dic["P3"] = new Dictionary<string, string>
            {
                { SystemConstant.ST10A,"WELD"},
                { SystemConstant.ST10B,"WELD"},
            };

            dic["P4"] = new Dictionary<string, string>
            {
                { SystemConstant.ST20A,"WELD"},
                { SystemConstant.ST20B,"WELD"},
            };

            dic["P5"] = new Dictionary<string, string>
            {
                { SystemConstant.ST30A,"WELD"},
                { SystemConstant.ST30B,"WELD"},
            };

            dic["Rivet"] = new Dictionary<string, string>
            {
                { SystemConstant.ST05B,"RIVET"},
            };

            if (dic.TryGetValue(plcName, out Dictionary<string, string> operationDic))
            {
                if (operationDic.TryGetValue(StationName, out string value))
                    return value;
            }
            return null;
        }

        /// <summary>
        /// 根据工位获取字段数据
        /// </summary>
        /// <param name="station"></param>
        /// <returns></returns>
        public static Dictionary<string, int> GetWeldOff(string station)
        {
            Dictionary<string, Dictionary<string, int>> dic = new Dictionary<string, Dictionary<string, int>>();

            dic["ST10A"] = new Dictionary<string, int>()
            {
                { "Off",8},
                { "SeamNo",130},
                { "StartTime",132},
                { "EndTime",140},
                { "WeldI",148},
                { "WeldU",548},
                { "WireSpeed",948},
                { "GasFlow",1348},
                { "Job",0},
            };

            dic["ST10B"] = new Dictionary<string, int>()
            {
                { "Off",12},
                { "SeamNo",13204},
                { "StartTime",13206},
                { "EndTime",13214},
                { "WeldI",13222},
                { "WeldU",13622},
                { "WireSpeed",14022},
                { "GasFlow",14422},
                { "Job",160},
            };

            dic["ST20A"] = new Dictionary<string, int>()
            {
                { "Off",8},
                { "SeamNo",130},
                { "StartTime",132},
                { "EndTime",140},
                { "WeldI",148},
                { "WeldU",548},
                { "WireSpeed",948},
                { "GasFlow",1348},
                { "Job",0},
            };

            dic["ST20B"] = new Dictionary<string, int>()
            {
                { "Off",10},
                { "SeamNo",13204},
                { "StartTime",13206},
                { "EndTime",13214},
                { "WeldI",13222},
                { "WeldU",13622},
                { "WireSpeed",14022},
                { "GasFlow",14422},
                { "Job",160},
            };

            dic["ST30A"] = new Dictionary<string, int>()
            {
                { "Off",16},
                { "SeamNo",130},
                { "StartTime",132},
                { "EndTime",140},
                { "WeldI",148},
                { "WeldU",548},
                { "WireSpeed",948},
                { "GasFlow",1348},
                { "Job",0},
            };

            dic["ST30B"] = new Dictionary<string, int>()
            {
                { "Off",16},
                { "SeamNo",26148},
                { "StartTime",26150},
                { "EndTime",26158},
                { "WeldI",26166},
                { "WeldU",26566},
                { "WireSpeed",26966},
                { "GasFlow",27366},
                { "Job",320},
            };

            if (dic.TryGetValue(station, out Dictionary<string, int> value))
                return value;
            return null;
        }

        /// <summary>
        /// 获取已读数据
        /// </summary>
        /// <param name="stationStatus"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public static Tuple<int, int> GetReturnBytesOff(string stationStatus)
        {
            var dic = new Dictionary<string, Tuple<int, int>>();
            #region 状态读取数组
            dic["P3-STATUS"] = new Tuple<int, int>(5001, 8);
            dic["P4-STATUS"] = new Tuple<int, int>(5001, 8);
            dic["P5-STATUS"] = new Tuple<int, int>(5001, 8);
            dic["Rivet-STATUS"] = new Tuple<int, int>(5001, 3);
            #endregion

            if (dic.TryGetValue(stationStatus, out Tuple<int, int> value))
                return value;
            return null;
        }

        /// <summary>
        /// 根据工站获取已读信号
        /// </summary>
        /// <param name="station"></param>
        public static int GetReadOff(string station)
        {
            Dictionary<string, int> keyValuePairs = new Dictionary<string, int>()
            {
                { "ST10A",0},
                { "ST10B",1},
                { "ST20A",0},
                { "ST20B",1},
                { "ST30A",0},
                { "ST30B",1},
                { SystemConstant.ST05B,0},
            };


            if (keyValuePairs.TryGetValue(station, out int value))
                return value;
            return 1000;
        }

        /// <summary>
        /// 根据物料编码查找工单位置索引
        /// </summary>
        /// <param name="wo_code"></param>
        /// <returns></returns>
        public static int GetOrderIndex(string material_id)
        {
            /*Dictionary<string, int> dic = new Dictionary<string, int>()
            {
                { "B04055698600",1},
                { "A04055698600",1},
                { "A04055698600",1},
            };

            if (dic.TryGetValue(material_id, out int value))
            {
                return value;
            }*/

            return 1;
        }

        /// <summary>
        /// 判断是否是否为该程序管辖的线体
        /// </summary>
        /// <param name="material_id"></param>
        /// <returns></returns>
        public static bool IsExistLine(string material_id)
        {
            List<string> lines = new List<string>()
            {
                "100401002095",
                "100401002101",
            };

            if (lines.Contains(material_id))
                return true;
            return false;
        }

        /// <summary>
        /// 根据物料编码获取PlcName
        /// </summary>
        /// <param name="material_id"></param>
        /// <returns></returns>
        public static List<string> GetPlcNameByMaterial(string material_id)
        {
            Dictionary<string, List<string>> dic = new Dictionary<string, List<string>>()
            {
                {"100401002095",new List<string>{ "P3", "P4"} },
                {"100401002101",new List<string>{ "P5", "Rivet" } },
            };

            if (dic.TryGetValue(material_id, out List<string> plcName))
            {
                return plcName;
            }

            return null;
        }

        /// <summary>
        /// 获取返回偏移映射
        /// </summary>
        /// <param name="plcName"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static int GetPartResultOff(string plcName, string name)
        {
            Dictionary<string, Dictionary<string, int>> dic = new Dictionary<string, Dictionary<string, int>>();

            dic["P1"] = new Dictionary<string, int>
            {
                { "Code1",1},
                { "Code2",2},
                { "Code3",3},
                { "Code4",4},
                { "Code5",5},
            };

            dic["P2"] = new Dictionary<string, int>
            {
                { "Code1",1},
                { "Code2",2},
                { "Code3",3},
                { "Code4",4},
                { "Code5",5},
            };

            dic["P3"] = new Dictionary<string, int>
            {
                { "Code1",1},
            };

            if (dic.TryGetValue(plcName, out Dictionary<string, int> value))
            {
                if (value.TryGetValue(name, out int off))
                    return off;
            }

            return 1000;
        }

        /// <summary>
        /// 获取批次
        /// </summary>
        /// <param name="material_id"></param>
        /// <returns></returns>
        public static string IsBatch(string material_id)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>()
            {
                { "1","ST10A"},
                { "2","ST10A"},
                { "3","ST20A"},
                { "4","ST20A"},
                { "5","ST05"},
            };

            if (dic.TryGetValue(material_id, out string value))
            {
                return value;
            }

            return null;
        }

        /// <summary>
        /// 根据工位获取对应工序号
        /// </summary>
        /// <param name="station"></param>
        /// <returns></returns>
        public static string GetOperationId(string station)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>()
            {
                { SystemConstant.ST10A,"A151"},
                { SystemConstant.ST10B,"A236"},
                { SystemConstant.ST20A,"A151"},
                { SystemConstant.ST20B,"A236"},
                { SystemConstant.ST30A,"A151"},
                { SystemConstant.ST30B,"A151"},
                { SystemConstant.ST05B,"A077"},
                { SystemConstant.ST05A,"A075"},
            };

            if (dic.TryGetValue(station, out string value))
            {
                return value;
            }

            return "未映射";
        }

        /// <summary>
        /// 判断是否是最后一个工位
        /// </summary>
        /// <param name="station"></param>
        /// <returns></returns>
        public static bool GetEndStation(string station)
        {
            List<string> strings = new List<string>()
            {
                SystemConstant.ST10B,
                SystemConstant.ST20B,
                SystemConstant.ST30A,
                SystemConstant.ST30B,
            };

            if (strings.Contains(station))
                return true;
            return false;
        }

        /// <summary>
        /// 获取实际焊缝编号
        /// </summary>
        /// <param name="seamNo"></param>
        /// <returns></returns>
        public static string GetRealSeamNo(string seamNo, string station, string robot)
        {
            string no = $"{robot}_{seamNo}";

            Dictionary<string, Dictionary<string, string>> dic = new Dictionary<string, Dictionary<string, string>>();

            dic["ST10A"] = new Dictionary<string, string>()
            {
                { "R1_1","B2"},
                { "R1_2","B4"},
                { "R1_3","B3"},
                { "R1_4","B1"},
                { "R2_1","A4"},
                { "R2_2","A2"},
                { "R2_3","A3"},
                { "R2_4","A1"},
            };

            dic["ST10B"] = new Dictionary<string, string>()
            {
                { "R1_1","R3"},
                { "R1_2","R1"},
                { "R1_3","R5"},
                { "R1_4","R4"},
                { "R1_5","R2"},
                { "R2_1","L3"},
                { "R2_2","L1"},
                { "R2_3","L5"},
                { "R2_4","L4"},
                { "R2_5","L2"},
            };

            dic["ST20A"] = new Dictionary<string, string>()
            {
                { "R1_1","B2"},
                { "R1_2","B4"},
                { "R1_3","B3"},
                { "R1_4","B1"},
                { "R2_1","A4"},
                { "R2_2","A2"},
                { "R2_3","A3"},
                { "R2_4","A1"},
            };

            dic["ST20B"] = new Dictionary<string, string>()
            {
                { "R1_1","R3"},
                { "R1_2","R1"},
                { "R1_3","R5"},
                { "R1_4","R4"},
                { "R1_5","R2"},
                { "R2_1","L3"},
                { "R2_2","L1"},
                { "R2_3","L5"},
                { "R2_4","L4"},
                { "R2_5","L2"},
            };

            dic["ST30A"] = new Dictionary<string, string>()
            {
                { "R1_1","D4"},
                { "R1_2","C1"},
                { "R1_3","B1"},
                { "R1_4","C4"},
                { "R1_5","D2"},
                { "R1_6","B3"},
                { "R1_7","A1"},
                { "R1_8","A2"},
                { "R2_1","D1"},
                { "R2_2","C3"},
                { "R2_3","B4"},
                { "R2_4","C2"},
                { "R2_5","D3"},
                { "R2_6","B2"},
                { "R2_7","A4"},
                { "R2_8","A3"},
            };

            dic["ST30B"] = new Dictionary<string, string>()
            {
                { "R1_1","D4"},
                { "R1_2","C1"},
                { "R1_3","B1"},
                { "R1_4","C4"},
                { "R1_5","D2"},
                { "R1_6","B3"},
                { "R1_7","A1"},
                { "R1_8","A2"},
                { "R2_1","D1"},
                { "R2_2","C3"},
                { "R2_3","B4"},
                { "R2_4","C2"},
                { "R2_5","D3"},
                { "R2_6","B2"},
                { "R2_7","A4"},
                { "R2_8","A3"},
            };

            if (dic.TryGetValue(station, out Dictionary<string, string> value))
                if (value.TryGetValue(no, out string result))
                    return result;

            return null;
        }

        /// <summary>
        /// 判断是否是后防物料
        /// </summary>
        /// <param name="materialId"></param>
        /// <returns></returns>
        public static bool GetP3P4Material(string materialId)
        {
            if (materialId.Equals("100401002095"))
                return true;
            return false;
        }

        /// <summary>
        /// 根据零件号获取防重码报警偏移量
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static (int startErrorOff, int ErrorBit) GetErrorOff(string plcName,string code)
        {
            // 提取零件码标识
            string Lac = code.Split(' ')[0];

            Dictionary<string, Dictionary<string, Tuple<int, int>>> dic = new Dictionary<string, Dictionary<string, Tuple<int,int>>>();

            dic["P3"] = new Dictionary<string, Tuple<int, int>>()
            {
                { "LAC-56011040",new Tuple<int,int>(264,0)},
                { "LAC-56011043",new Tuple<int,int>(264,1)},
                { "LAC-56011027",new Tuple<int,int>(264,2)},
            };

            dic["P4"] = new Dictionary<string, Tuple<int, int>>()
            {
                { "LAC-56011040",new Tuple<int,int>(264,0)},
                { "LAC-56011043",new Tuple<int,int>(264,1)},
                { "LAC-56011027",new Tuple<int,int>(264,2)},
            };

            dic["P5"] = new Dictionary<string, Tuple<int, int>>()
            {
                { "LAC-84001051",new Tuple<int,int>(264,0)},
            };

            dic["Rivet"] = new Dictionary<string, Tuple<int, int>>()
            {
                { "LAC-84001051",new Tuple<int,int>(265,0)},
                // 
                { "LAC-84001052-1",new Tuple<int,int>(265,1)},
            };

            if(dic.TryGetValue(plcName, out var value))
            {
                if(value.TryGetValue(Lac, out var tupleValue))
                    return (tupleValue.Item1, tupleValue.Item2);
            }

            return (1000, 1000);
        }
        /// <summary>
        /// 已核对
        /// </summary>
        /// <param name="byte"></param>
        /// <param name="sty"></param>
        /// <returns></returns>
        public static (string errorType, string errorMessage) GetP5ErrorMessage(int @byte, int sty)
        {
            Dictionary<string, (string, string)> dic = new Dictionary<string, (string, string)>();



            dic[$"{2}.{0}"] = ("机器人故障", "P5-R1机器人故障");
            dic[$"{4}.{0}"] = ("机器人故障", "P5-R2机器人故障");

            dic[$"{102}.{0}"] = ("左侧夹具故障", "P5-STA20_JIG报错");

            dic[$"{110}.{0}"] = ("右侧夹具故障", "P5-STA30_JIG报错");

            dic[$"{118}.{0}"] = ("左侧夹具传感器故障", "P5-STA20_JIG_SEN报错");

            dic[$"{126}.{0}"] = ("右侧夹具传感器故障", "P5-STA30_JIG_SEN报错");

            dic[$"{142}.{0}"] = ("安全门故障", "P5-安全门打开");

            dic[$"{144}.{0}"] = ("光栅故障", "P5-光栅被闯入");

            dic[$"{152}.{0}"] = ("急停", "P5-HMI急停");
            dic[$"{152}.{1}"] = ("急停", "P5-OB盒急停");
            dic[$"{152}.{2}"] = ("急停", "P5-安全门急停");
            dic[$"{152}.{3}"] = ("急停", "P5-R1急停");
            dic[$"{152}.{4}"] = ("急停", "P5-R2急停");

            dic[$"{166}.{0}"] = ("通讯故障", "P5-CPU通讯故障");

            dic[$"{167}.{2}"] = ("通讯故障", "P5-HMI1通讯故障");

            dic[$"{169}.{0}"] = ("通讯故障", "P5-MCP-ET200通讯故障");
            dic[$"{169}.{2}"] = ("通讯故障", "P5-ST30-AJ2-A-ET200通讯故障");
            dic[$"{169}.{3}"] = ("通讯故障", "P5-ST30-AJ2-B-ET200通讯故障");

            dic[$"{178}.{4}"] = ("通讯故障", "P5-A面阀岛通讯故障");
            dic[$"{178}.{5}"] = ("通讯故障", "P5-B面阀岛通讯故障");

            dic[$"{181}.{0}"] = ("通讯故障", "P5-R01通讯故障");
            dic[$"{181}.{1}"] = ("通讯故障", "P5-R02通讯故障");


            if (dic.TryGetValue($"{@byte}.{sty}", out var value))
            {
                return value;
            }

            return ("", "");
        }

        /// <summary>
        /// 已核对
        /// </summary>
        /// <param name="byte"></param>
        /// <param name="sty"></param>
        /// <returns></returns>
        public static (string errorType, string errorMessage) GetP4ErrorMessage(int @byte, int sty)
        {
            Dictionary<string, (string, string)> dic = new Dictionary<string, (string, string)>();



            dic[$"{2}.{0}"] = ("机器人故障", "P4-R1机器人故障");
            dic[$"{4}.{0}"] = ("机器人故障", "P4-R2机器人故障");

            dic[$"{102}.{0}"] = ("左侧夹具故障", "P4-STA10_JIG报错");

            dic[$"{110}.{0}"] = ("右侧夹具故障", "P4-STA20_JIG报错");

            dic[$"{118}.{0}"] = ("左侧夹具传感器故障", "P4-STA10_JIG_SEN报错");

            dic[$"{126}.{0}"] = ("右侧夹具传感器故障", "P4-STA20_JIG_SEN报错");

            dic[$"{142}.{0}"] = ("安全门故障", "P4-安全门打开");

            dic[$"{144}.{0}"] = ("光栅故障", "P4-上件口光栅被闯入");


            dic[$"{152}.{0}"] = ("急停", "P4-HMI急停");
            dic[$"{152}.{1}"] = ("急停", "P4-OB盒急停");
            dic[$"{152}.{2}"] = ("急停", "P4-安全门急停");
            dic[$"{152}.{3}"] = ("急停", "P4-R1急停");
            dic[$"{152}.{4}"] = ("急停", "P4-R2急停");

            dic[$"{166}.{0}"] = ("通讯故障", "P4-CPU通讯故障");
  
            dic[$"{167}.{2}"] = ("通讯故障", "P4-HMI1通讯故障");

            dic[$"{169}.{0}"] = ("通讯故障", "P4-MCP-ET200通讯故障");
            dic[$"{169}.{1}"] = ("通讯故障", "P4-STA10-ET200通讯故障");
            dic[$"{169}.{2}"] = ("通讯故障", "P4-STA20-ET200通讯故障");

            dic[$"{178}.{3}"] = ("通讯故障", "P4-STA10-PM通讯故障");
            dic[$"{178}.{4}"] = ("通讯故障", "P4-STA20-PM通讯故障");

            dic[$"{181}.{0}"] = ("通讯故障", "P4-R1通讯故障");
            dic[$"{181}.{1}"] = ("通讯故障", "P4-R2通讯故障");


            if (dic.TryGetValue($"{@byte}.{sty}", out var value))
            {
                return value;
            }

            return ("", "");
        }

        /// <summary>
        /// 已核对
        /// </summary>
        /// <param name="byte"></param>
        /// <param name="sty"></param>
        /// <returns></returns>
        public static (string errorType, string errorMessage) GetP3ErrorMessage(int @byte, int sty)
        {
            Dictionary<string, (string, string)> dic = new Dictionary<string, (string, string)>();

            dic[$"{2}.{0}"] = ("机器人故障", "P3-ROB01&ROB02状态");
            //dic[$"{4}.{0}"] = ("机器人故障", "R2机器人故障");

            dic[$"{102}.{0}"] = ("左侧夹具故障", "P3-STA10_JIG报错");

            dic[$"{110}.{0}"] = ("右侧夹具故障", "P3-STA20_JIG报错");

            dic[$"{118}.{0}"] = ("左侧夹具传感器故障", "P3-STA10_JIG_SEN报错");

            dic[$"{126}.{0}"] = ("右侧夹具传感器故障", "P3-STA20_JIG_SEN报错");

            dic[$"{142}.{0}"] = ("安全门故障", "P3-SD1安全门打开");

            dic[$"{144}.{0}"] = ("光栅故障", "P3-上件口光栅被闯入");


            dic[$"{152}.{0}"] = ("急停", "P3-HMI急停");
            dic[$"{152}.{1}"] = ("急停", "P3-操作盒急停");
            dic[$"{152}.{2}"] = ("急停", "P3-安全门急停");
            dic[$"{152}.{3}"] = ("急停", "P3-机器人急停");

            dic[$"{166}.{0}"] = ("通讯故障", "P3-CPU通讯故障");

            dic[$"{167}.{2}"] = ("通讯故障", "P3-HMI1通讯故障");

            dic[$"{169}.{0}"] = ("通讯故障", "P3-MCP-ET200通讯故障");
            dic[$"{169}.{1}"] = ("通讯故障", "P3-STA10-ET200通讯故障");
            dic[$"{169}.{2}"] = ("通讯故障", "P3-STA20-ET200通讯故障");

            dic[$"{178}.{3}"] = ("通讯故障", "P3-STA10-PM通讯故障");
            dic[$"{178}.{4}"] = ("通讯故障", "P3-STA20-PM通讯故障");

            dic[$"{181}.{0}"] = ("通讯故障", "P3-R1&2通讯故障");



            if (dic.TryGetValue($"{@byte}.{sty}", out var value))
            {
                return value;
            }

            return ("", "");
        }

    

    }
}